package com.tangruojie.optional;

import com.tangruojie.entity.Author;
import com.tangruojie.entity.Book;
import com.tangruojie.stream.StreamDemoTest;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

/**
 * 转换 map
 */
public class OptionalDemo06 {

    public static void main(String[] args) {

        List<Author> authors = StreamDemoTest.getAuthors();

        Optional<List<Author>> optionalAuthors = Optional.of(authors);
        Optional<List<Book>> books = optionalAuthors.map(new Function<List<Author>, List<Book>>() {
            @Override
            public List<Book> apply(List<Author> authors) {
                return authors.stream()
                        .flatMap(author -> author.getBooks().stream())
                        .toList();
            }
        });
        books.stream()
                .flatMap(Collection::stream)
                .map(Book::getName)
                .distinct()
                .forEach(System.out::println);

    }

}
